package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016092200568607";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDJ26Ck7sXTnPpmNAtVJjJ94OL7DLwn/iiJoqPMao6ZU6NMenl+aUtKCFAHdtQSHAmDkbnHeLNAoADsdfZWypGmZ4FBpYltyAycstSynHbZ7qAk3hkhkm8FjmoypP8sKYeA2XhNnCm1UHjFsKbgubjKOjbmxCVtjxYaSBfe6mWVjwzUL84ZpCdtaYFIWLyLjyMvLPj/Fl8Ud5/WUtvQRJh4iShHvGZJ7v4CuSOm3yD1Bo51qNf/HkPQgA56Cu07BVbH/DajuhyQG3D6pdY4uQV2+RGHAvJWasIEz4QdWO5RRqmcNXET4SV28Sjl7kz0+GfUo5Lwr0BuifUjHz1bL90jAgMBAAECggEACQcUiSvQlyU3IakENLuvmGedXyLzq0o3n+gxMSw20m6mJ/52PowQ/O+gPKU42NJ+Lm8vAtfIFITTZb/k037dwrZwVnJD0jAXOE7bKl3LBvYgHWX9c73rzzDOWRN+AgidccrIEZiBWQWPUTAUVoqX1BuEZw+wqNS0ZVqCDssm3ehidwUcbtjZ5TapFmpnedmublKNducmaRro5gxOmt8wZmk3hBAu+JHouXvkweOvDJfl+g/XMJONwggABH6gqtQzKyzMBJzJDxZuXzrgzKibX9FJJA+4tbb0IdTcXzNedUEA7RpTT8tKHUONV1lt3JtSpmjlUBNPJaWBPmpKAOpBMQKBgQD7wG/6ihWRYgeGzZl/yb468SB5zeROufQuEtTM2K2SLuqMVLX4FIDi9gZMDUJDgH//sb+bhiClAWmnqLm5fSai/IyD3LRnqhZQJ3OmJdbsDUL7wipgAYNmMTnwxDoAz+ji0Y0UgkVLVX37lXodN2CbXA8v3SktqoSsxVjDPiySKwKBgQDNQ6ZeybO6opxiUp0u706+JKI4UV8EikL8SsR8kOS8QZ4XbT49OzSFfESYYhym7e+UnuTY3bf6uquqSjrUD+VXLd+91YnL98uHkarkY3btcVb5FyJlT/Ri1Sg0k5NMIT/jx6qCSkj46gRJcnM1ELoDsdQVbnrfwwDAHpSRanZ86QKBgE9PEnfVJHJGdaSMBL1zKtCaweZdbH04q6oQGKXn5NZWnWFUFnduziOx6c3vp15ojupYbspp+MLCrSw6RTggKxnRgdrQnqzBsoHRy+cdqJV4oA973amdEG4TN/OXfcZfuTgrV8yMNZRD/IpTTsMBfRe0arxjaeOPAnkDOrFXVuc9AoGBAIqK4J4VsdOEsdzDOghVyWYZP8yXT3S3BWNX66rP+TL/bv8M4vddIKQALiRUBFa/PfeoD3o4nFcE65zyoSZE3FytP9jXajxbMep87UVdxQWafEN/091hy+dxS8PA36iAwKizOWjbTsmC2gXg09W7RnH78tPMT+77pWQGv+wuWW8RAoGAeenZ2FCAbkxkpyDhnhE9YK5FL6P2HpdneXSt31b2XT1YSSh0Mh1dt8z0IONhTFv5duAnoxapUC0nOOEy1cXxLSCnJh6CdPB8voCipzJbUD6+SvfkKSbP68CBJiMA6jEad3OgR+2cIByZhkSwJlJzwXOiQH8Zjdr08e3eguh1P7s=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgD9oKFMvaPARUYFAxKtVAXQVDRIr6FlegxuyIY4Ne1vSJhVWPVS5onfTot2kL83NaGIXWshjFtWJD4RB+wrUUAAM1p66TWvD62Kw8huDZIEVyAk0dR2OSkmTBgQ3EBKXO5BFRlgV5TWXUgCC582CDNJFOB2lpePorN1WFxl5RExPLe6xZY9tXQs/P0GJEFKCFoyYvZ+lp6dccjoxrVtVkHewrZnSpFMIYW469TV4umdhDnCD1d/CVP/sI6khoxNGDTZteziCYWWZBZc9jeNQdPU9GHY+TkYydqLqHK7c49CbD7gFEI8Q/UZX12okYThcQjyqeDxAjM8KoAmc15hhkwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://member.gulimall.com/memberOrder.html";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";
    // 订单超时时间，到达超时时间后自动关闭订单不能再继续支付
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // timeout_express 订单支付超时时间
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;
    }
}
